
Partial Class admin_appSupportBroker
    Inherits System.Web.UI.Page
    Private sValue As String

    Protected Sub Page_Load(ByVal sender As Object, _
                            ByVal e As System.EventArgs) _
                            Handles Me.Load
        ClearStatus()
        SetupJavaScript()
        If Not Page.IsPostBack Then
            Me.SupportBrokerLabel.Text = EntityController.GetSingleDisplayName("Support Broker")
            Me.SupportBrokerScrollingGridView.Sort("AgencyName", SortDirection.Ascending)
        End If

    End Sub

    Protected Sub AddAgencyButton_Click(ByVal sender As Object, _
                                        ByVal e As System.EventArgs) _
                                        Handles AddAgencyButton.Click

        SupportBrokerScrollingGridView.Visible = False
        AddAgencyButton.Visible = False
        SubmitButton.Visible = True
        CancelButton.Visible = True
        Me.SupportBrokerDetailsView.Visible = True
        Me.DetailsPanel.Visible = True
        Me.ClearForm()
        Me.hfMode.Value = "INSERT"
        Me.hfSupportBrokerAgencyId.Value = 0

    End Sub

    Protected Sub DetailsView1_ItemInserting(ByVal sender As Object, _
                                             ByVal e As System.Web.UI.WebControls.DetailsViewInsertEventArgs) _
                                             Handles SupportBrokerDetailsView.ItemInserting

        Me.SupportBrokerDetailsView.Visible = False
        Me.DetailsPanel.Visible = False

    End Sub

    Protected Sub CancelButton_Click(ByVal sender As Object, _
                                     ByVal e As System.EventArgs) _
                                     Handles CancelButton.Click

        SubmitButton.Visible = False
        CancelButton.Visible = False
        Me.SupportBrokerDetailsView.Visible = False
        Me.DetailsPanel.Visible = False
        Me.ErrorSummary.Visible = False
        AddAgencyButton.Visible = True
        SupportBrokerScrollingGridView.Visible = True
        Me.ClearForm()
        SetStatus("Action cancelled.")

    End Sub

    Protected Sub SubmitButton_Click(ByVal sender As Object, _
                                     ByVal e As System.EventArgs) _
                                     Handles SubmitButton.Click

        Dim fieldName As TextBox = CType(SupportBrokerDetailsView.FindControl("FieldNameTextBox"), TextBox)
        Dim fieldProvider As TextBox = CType(SupportBrokerDetailsView.FindControl("ProviderNumberTextBox"), TextBox)
        Dim fieldCase As TextBox = CType(SupportBrokerDetailsView.FindControl("CaseLimitTextBox"), TextBox)
        Dim txtSA1 As TextBox = CType(SupportBrokerDetailsView.FindControl("txtSA1"), TextBox)
        Dim txtSA2 As TextBox = CType(SupportBrokerDetailsView.FindControl("txtSA2"), TextBox)
        Dim txtSA3 As TextBox = CType(SupportBrokerDetailsView.FindControl("txtSA3"), TextBox)
        Dim txtCity As TextBox = CType(SupportBrokerDetailsView.FindControl("txtSACity"), TextBox)
        Dim txtState As TextBox = CType(SupportBrokerDetailsView.FindControl("txtSAState"), TextBox)
        Dim txtZip As TextBox = CType(SupportBrokerDetailsView.FindControl("txtSAZip"), TextBox)
        Dim fieldActive As CheckBox = CType(SupportBrokerDetailsView.FindControl("ActiveCheckBox"), CheckBox)
        Dim txtContactFirstName As TextBox = CType(SupportBrokerDetailsView.FindControl("txtContactFirstName"), TextBox)
        Dim txtContactLastName As TextBox = CType(SupportBrokerDetailsView.FindControl("txtContactLastName"), TextBox)
        Dim phoneNumberInput As InputControls.PhoneInputControl = CType(SupportBrokerDetailsView.FindControl("PhoneNumberInputControl"), InputControls.PhoneInputControl)
        Dim faxNumberInput As InputControls.PhoneInputControl = CType(SupportBrokerDetailsView.FindControl("FaxNumberInputControl"), InputControls.PhoneInputControl)
        Dim txtEmailAddress As TextBox = CType(SupportBrokerDetailsView.FindControl("txtEmailAddress"), TextBox)
        Dim ddlCounty As DropDownList = CType(SupportBrokerDetailsView.FindControl("ddlCounty"), DropDownList)
        Dim phoneNumber As String
        Dim faxNumber As String

        Dim hasAddress As Boolean = False
        Dim supportBrokerAgencyId As Integer

        phoneNumber = StripPhoneNumber(phoneNumberInput.Value)
        faxNumber = StripPhoneNumber(faxNumberInput.Value)

        If fieldProvider.Text = "" Then
            sValue = "You need to enter a Medicaid Provider Number before continuing."
            'Me.ErrorSummary.SetSingleError(sValue)
            'Me.ErrorSummary.Visible = True
            SetStatus(sValue)
            Exit Sub
        End If

        If fieldName.Text = "" Then
            sValue = "You need to enter a value for Agency Name before continuing."
            'Me.ErrorSummary.SetSingleError(sValue)
            'Me.ErrorSummary.Visible = True
            SetStatus(sValue)
            Exit Sub
        End If

        If fieldCase.Text = "" Then
            sValue = "You need to enter a case load limit before continuing."
            'Me.ErrorSummary.SetSingleError(sValue)
            'Me.ErrorSummary.Visible = True
            SetStatus(sValue)
            Exit Sub
        End If

        If Not IsNumeric(fieldCase.Text) Then
            sValue = "Case load needs to be a valid number."
            'Me.ErrorSummary.SetSingleError(sValue)
            'Me.ErrorSummary.Visible = True
            SetStatus(sValue)
            Exit Sub
        End If

        If phoneNumber = "" Then
            sValue = "Telephone Number needs to be a valid phone number."
            'Me.ErrorSummary.SetSingleError(sValue)
            'Me.ErrorSummary.Visible = True
            SetStatus(sValue)
            Exit Sub
        End If

        ''If faxNumber = "" Then
        'sValue = "Fax Number needs to be a valid phone number."
        'Me.ErrorSummary.SetSingleError(sValue)
        'Me.ErrorSummary.Visible = True
        'SetStatus(sValue)
        'Exit Sub
        'End If

        'CHEK IF THE USER ENTERED ANY PART OF AN ADDRESS
        If (txtSA1.Text.Trim().Length > 0 Or txtSA2.Text.Trim().Length > 0 Or txtSA3.Text.Trim().Length > 0 Or _
            txtCity.Text.Trim().Length > 0 Or txtState.Text.Trim().Length > 0 Or txtZip.Text.Trim().Length > 0) Then

            'VALIDATE THE ADDRESS
            If Not Me.ValidateAddress(txtSA1, txtSA2, txtSA3, txtCity, txtState, txtZip) Then
                sValue = "You need to enter a complete address before continuing."
                'Me.ErrorSummary.SetSingleError(sValue)
                'Me.ErrorSummary.Visible = True
                SetStatus(sValue)
                Exit Sub
            Else
                hasAddress = True
            End If

        End If

        supportBrokerAgencyId = CType(Me.hfSupportBrokerAgencyId.Value, Integer)

        'CHECK IF AN AGENCY BY THIS NAME ALREADY EXISTS
        If Cdm.DataCore.Entities.SupportBrokerAgency.Exists(supportBrokerAgencyId, fieldName.Text) Then
            sValue = "A Support Broker Agency by that name already exists. Please enter a different name."
            'Me.ErrorSummary.SetSingleError(sValue)
            'Me.ErrorSummary.Visible = True
            SetStatus(sValue)
            Exit Sub
        End If

        SubmitButton.Visible = False
        CancelButton.Visible = False
        SupportBrokerDetailsView.Visible = False
        Me.DetailsPanel.Visible = False

        Dim oSBA As Cdm.DataCore.Entities.SupportBrokerAgency

        If Me.hfMode.Value.ToUpper() = "EDIT" Then
            oSBA = Cdm.DataCore.Entities.SupportBrokerAgency.Retrieve(supportBrokerAgencyId)
        Else
            oSBA = New Cdm.DataCore.Entities.SupportBrokerAgency()
        End If

        With oSBA
            .MedicaidNumber = fieldProvider.Text.Trim()
            .AgencyName = fieldName.Text.Trim()
            .Active = fieldActive.Checked
            .CaseLimit = fieldCase.Text.Trim()
            .Address1 = txtSA1.Text.Trim()
            .Address2 = txtSA2.Text.Trim()
            .Address3 = txtSA3.Text.Trim()
            .City = txtCity.Text.Trim()
            .State = txtState.Text.Trim()
            .Zip = txtZip.Text.Trim()
            .ContactFirstName = txtContactFirstName.Text.Trim()
            .ContactLastName = txtContactLastName.Text.Trim()
            .PhoneNumber = phoneNumber
            .FaxNumber = faxNumber
            .EmailAddress = txtEmailAddress.Text.Trim()
            .CountyId = ddlCounty.SelectedValue.ToString().Trim()
            .DateModified = Now()
            .LastModified = UserAuthorization.CurrentUser.LogonId
            .Persist()
        End With

        Me.SupportBrokerObjectDataSource.Select()
        SupportBrokerScrollingGridView.DataBind()

        Me.ClearForm()

        If ErrorSummary.Visible = True Then
            ErrorSummary.Visible = False
        End If
        SupportBrokerScrollingGridView.Visible = True
        AddAgencyButton.Visible = True

        If Me.hfMode.Value.ToUpper() = "EDIT" Then
            SetStatus("Update Successful.")
        Else
            SetStatus("Agency Added.")
        End If

    End Sub


    Private Sub SetupJavaScript()

        EntityController.JavaScriptAddChildDiv(Me.SupportBrokerScrollingGridView.DivClientId, CInt(Me.SupportBrokerScrollingGridView.ToolTip))
        EntityController.JavaScriptAddChildDiv(Me.DetailsPanel.ClientID, 90)
        ClientScript.RegisterStartupScript(Me.GetType(), "scrollDivs", EntityController.JavaScriptScrollingGridStartup(Me.SupportBrokerScrollingGridView), True)
        EntityController.JavaScriptSetFixedHeight(28)
    End Sub

    Private Sub ClearStatus()
        SetStatus("")
    End Sub
    Private Sub SetStatus(ByVal message As String)
        Dim mymaster As ASP.admin_admin_master = CType(Me.Master, ASP.admin_admin_master)
        mymaster.SetStatus(message)
    End Sub

    Protected Function StripPhoneNumber(ByVal phoneNumberString As String) As String
        Dim counter As Integer
        Dim phoneNumber As String = ""
        For counter = 0 To phoneNumberString.Length - 1
            Try
                Convert.ToInt32(phoneNumberString.Substring(counter, 1))
                phoneNumber = phoneNumber + phoneNumberString.Substring(counter, 1)
            Catch ex As Exception
            End Try
        Next
        If phoneNumber.Length = 10 Then
            Return phoneNumber
        Else
            Return ""
        End If
    End Function

    Protected Function FormatPhoneNumber(ByVal phoneNumberString As String)
        Dim phoneNumber As String = ""
        Try
            phoneNumber = "(" + phoneNumberString.Substring(0, 3) + ")" + phoneNumberString.Substring(3, 3) + "-" + phoneNumberString.Substring(6)
        Catch ex As Exception
        End Try
        Return phoneNumber
    End Function


    Private Function ValidateAddress(ByVal SA1 As TextBox, ByVal SA2 As TextBox, ByVal SA3 As TextBox, ByVal City As TextBox, ByVal State As TextBox, ByVal Zip As TextBox) As Boolean

        If (SA1.Text.Trim().Length > 0 And City.Text.Trim().Length > 0 And State.Text.Trim().Length > 0 And Zip.Text.Trim().Length > 0) Then
            Return True
        Else
            Return False
        End If

    End Function

    Private Sub SetForm(ByVal supportBrokerAgencyId As Integer)

        Dim oSupportBrokerAgency As Cdm.DataCore.Entities.SupportBrokerAgency
        Dim fieldName As TextBox = CType(SupportBrokerDetailsView.FindControl("FieldNameTextBox"), TextBox)
        Dim fieldProvider As TextBox = CType(SupportBrokerDetailsView.FindControl("ProviderNumberTextBox"), TextBox)
        Dim fieldCase As TextBox = CType(SupportBrokerDetailsView.FindControl("CaseLimitTextBox"), TextBox)
        Dim txtSA1 As TextBox = CType(SupportBrokerDetailsView.FindControl("txtSA1"), TextBox)
        Dim txtSA2 As TextBox = CType(SupportBrokerDetailsView.FindControl("txtSA2"), TextBox)
        Dim txtSA3 As TextBox = CType(SupportBrokerDetailsView.FindControl("txtSA3"), TextBox)
        Dim txtCity As TextBox = CType(SupportBrokerDetailsView.FindControl("txtSACity"), TextBox)
        Dim txtState As TextBox = CType(SupportBrokerDetailsView.FindControl("txtSAState"), TextBox)
        Dim txtZip As TextBox = CType(SupportBrokerDetailsView.FindControl("txtSAZip"), TextBox)
        Dim fieldActive As CheckBox = CType(SupportBrokerDetailsView.FindControl("ActiveCheckBox"), CheckBox)
        Dim txtContactFirstName As TextBox = CType(SupportBrokerDetailsView.FindControl("txtContactFirstName"), TextBox)
        Dim txtContactLastName As TextBox = CType(SupportBrokerDetailsView.FindControl("txtContactLastName"), TextBox)
        Dim phoneNumberInput As InputControls.PhoneInputControl = CType(SupportBrokerDetailsView.FindControl("PhoneNumberInputControl"), InputControls.PhoneInputControl)
        Dim faxNumberInput As InputControls.PhoneInputControl = CType(SupportBrokerDetailsView.FindControl("FaxNumberInputControl"), InputControls.PhoneInputControl)
        Dim txtEmailAddress As TextBox = CType(SupportBrokerDetailsView.FindControl("txtEmailAddress"), TextBox)
        Dim ddlCounty As DropDownList = CType(SupportBrokerDetailsView.FindControl("ddlCounty"), DropDownList)

        oSupportBrokerAgency = Cdm.DataCore.Entities.SupportBrokerAgency.Retrieve(supportBrokerAgencyId)

        With oSupportBrokerAgency
            fieldName.Text = .AgencyName
            fieldProvider.Text = .MedicaidNumber
            fieldCase.Text = .CaseLimit
            txtSA1.Text = .Address1
            txtSA2.Text = .Address2
            txtSA3.Text = .Address3
            txtCity.Text = .City
            txtState.Text = .State
            txtZip.Text = .Zip
            txtContactFirstName.Text = .ContactFirstName
            txtContactLastName.Text = .ContactLastName
            phoneNumberInput.Value = .PhoneNumber
            faxNumberInput.Value = .FaxNumber
            txtEmailAddress.Text = .EmailAddress

            If Not ddlCounty.SelectedItem Is Nothing Then
                ddlCounty.SelectedItem.Selected = False
            End If
            If .CountyId <> 0 Then
                ddlCounty.Items.FindByValue(.CountyId).Selected = True
            Else
                ddlCounty.Items.FindByValue("0").Selected = True
            End If
            fieldActive.Checked = .Active
        End With

        Me.hfsupportBrokerAgencyId.Value = supportBrokerAgencyId
        Me.hfMode.Value = "EDIT"

    End Sub

    Private Sub ClearForm()

        Dim fieldName As TextBox = CType(SupportBrokerDetailsView.FindControl("FieldNameTextBox"), TextBox)
        Dim fieldProvider As TextBox = CType(SupportBrokerDetailsView.FindControl("ProviderNumberTextBox"), TextBox)
        Dim fieldCase As TextBox = CType(SupportBrokerDetailsView.FindControl("CaseLimitTextBox"), TextBox)
        Dim txtSA1 As TextBox = CType(SupportBrokerDetailsView.FindControl("txtSA1"), TextBox)
        Dim txtSA2 As TextBox = CType(SupportBrokerDetailsView.FindControl("txtSA2"), TextBox)
        Dim txtSA3 As TextBox = CType(SupportBrokerDetailsView.FindControl("txtSA3"), TextBox)
        Dim txtCity As TextBox = CType(SupportBrokerDetailsView.FindControl("txtSACity"), TextBox)
        Dim txtState As TextBox = CType(SupportBrokerDetailsView.FindControl("txtSAState"), TextBox)
        Dim txtZip As TextBox = CType(SupportBrokerDetailsView.FindControl("txtSAZip"), TextBox)
        Dim fieldActive As CheckBox = CType(SupportBrokerDetailsView.FindControl("ActiveCheckBox"), CheckBox)
        Dim txtContactFirstName As TextBox = CType(SupportBrokerDetailsView.FindControl("txtContactFirstName"), TextBox)
        Dim txtContactLastName As TextBox = CType(SupportBrokerDetailsView.FindControl("txtContactLastName"), TextBox)
        Dim phoneNumberInput As InputControls.PhoneInputControl = CType(SupportBrokerDetailsView.FindControl("PhoneNumberInputControl"), InputControls.PhoneInputControl)
        Dim faxNumberInput As InputControls.PhoneInputControl = CType(SupportBrokerDetailsView.FindControl("FaxNumberInputControl"), InputControls.PhoneInputControl)
        Dim txtEmailAddress As TextBox = CType(SupportBrokerDetailsView.FindControl("txtEmailAddress"), TextBox)
        Dim ddlCounty As DropDownList = CType(SupportBrokerDetailsView.FindControl("ddlCounty"), DropDownList)

        fieldName.Text = String.Empty
        fieldProvider.Text = String.Empty
        fieldCase.Text = String.Empty
        txtSA1.Text = String.Empty
        txtSA2.Text = String.Empty
        txtSA3.Text = String.Empty
        txtCity.Text = String.Empty
        txtState.Text = String.Empty
        txtZip.Text = String.Empty
        txtContactFirstName.Text = String.Empty
        txtContactLastName.Text = String.Empty
        phoneNumberInput.Value = String.Empty
        faxNumberInput.Value = String.Empty
        txtEmailAddress.Text = String.Empty
        fieldActive.Checked = True

    End Sub

    Protected Sub SupportBrokerScrollingGridView_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles SupportBrokerScrollingGridView.DataBound
        AccessibilityController.AddImageToSortableGridHeaders(SupportBrokerScrollingGridView)
    End Sub

    Protected Sub SupportBrokerScrollingGridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles SupportBrokerScrollingGridView.RowDataBound
        Dim activeCheckBox As CheckBox = e.Row.FindControl("IsActiveCheckBox")
        If Not activeCheckBox Is Nothing Then
            activeCheckBox.InputAttributes.Add("title", "check if Agency " & CType(e.Row.Cells(0).Controls(0), DataBoundLiteralControl).Text & " is active.")
        End If
    End Sub

    Protected Sub SupportBrokerScrollingGridView_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles SupportBrokerScrollingGridView.RowEditing

        Dim supportBrokerId As Integer = Me.SupportBrokerScrollingGridView.DataKeys(e.NewEditIndex()).Value

        SupportBrokerScrollingGridView.Visible = False
        AddAgencyButton.Visible = False

        SubmitButton.Visible = True
        CancelButton.Visible = True
        Me.SupportBrokerDetailsView.Visible = True
        Me.DetailsPanel.Visible = True

        Me.SetForm(supportBrokerId)
        e.Cancel = True
    End Sub
End Class
